package window;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;

import dao.TeacherDao;

import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;



public class Addpage {
	String id="";
	public JFrame addpageFrame;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;
	private JTextField textField_5;
	private JTextField textField_6;
	private JTextField textField_7;

	/**
	 * Launch the application.
	 */
	
	/**
	 * Create the application.
	 */
	public Addpage(String id) {
		this.id=id;
		
		initialize();
	}
	
	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		addpageFrame = new JFrame();
		addpageFrame.setBounds(-7, 0, 1380, 736);
		addpageFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		addpageFrame.getContentPane().setLayout(null);
		addpageFrame.setTitle("教师信息管理系统");
		ImageIcon icon=new ImageIcon("D:\\eclipse\\workspace\\TeacherInformationManagementSystem\\src\\window\\1.png");
		addpageFrame.setIconImage(icon.getImage());
		JLabel lblNewLabel = new JLabel("教师信息录入");
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel.setForeground(Color.DARK_GRAY);
		lblNewLabel.setFont(new Font("宋体", Font.BOLD, 30));
		lblNewLabel.setBounds(0, 50, 1364, 31);
		addpageFrame.getContentPane().add(lblNewLabel);
		
		JMenuBar menuBar = new JMenuBar();
		menuBar.setBounds(0, 0, 1364, 23);
		addpageFrame.getContentPane().add(menuBar);
		
		JMenu mnNewMenu = new JMenu("操作(ALT+1)");
		mnNewMenu.setHorizontalAlignment(SwingConstants.CENTER);
		mnNewMenu.setFont(new Font("幼圆", Font.PLAIN, 15));
		mnNewMenu.setForeground(Color.DARK_GRAY);
		mnNewMenu.setMnemonic('1');
		menuBar.add(mnNewMenu);
		
		JMenuItem menuItem = new JMenuItem("返回(F)");
		menuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				TeacherHomepage th=new TeacherHomepage(id);
				th.teacherHomepageFrame.setVisible(true);
				addpageFrame.dispose();
			}
		});
		menuItem.setFont(new Font("幼圆", Font.PLAIN, 13));
		menuItem.setForeground(Color.DARK_GRAY);
		menuItem.setMnemonic('F');
		mnNewMenu.add(menuItem);
		
		JMenuItem mntme = new JMenuItem("退出登录(E)");
		mntme.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Login lg=new Login();
				lg.LoginFrame.setVisible(true);
				addpageFrame.dispose();
			}
		});
		mntme.setMnemonic('E');
		mntme.setForeground(Color.DARK_GRAY);
		mntme.setFont(new Font("幼圆", Font.PLAIN, 13));
		mnNewMenu.add(mntme);
		
		JLabel lblNewLabel_1 = new JLabel("欢迎你，亲爱的");
		lblNewLabel_1.setForeground(Color.DARK_GRAY);
		lblNewLabel_1.setFont(new Font("幼圆", Font.PLAIN, 12));
		lblNewLabel_1.setBounds(10, 37, 90, 15);
		addpageFrame.getContentPane().add(lblNewLabel_1);
		
		JLabel lblNewLabel_2 = new JLabel("New label");
		lblNewLabel_2.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel_2.setText(id);
		lblNewLabel_2.setForeground(Color.DARK_GRAY);
		lblNewLabel_2.setFont(new Font("幼圆", Font.BOLD, 13));
		lblNewLabel_2.setBounds(98, 37, 75, 15);
		addpageFrame.getContentPane().add(lblNewLabel_2);
		
		JLabel label = new JLabel("工号：");
		label.setForeground(Color.DARK_GRAY);
		label.setFont(new Font("幼圆", Font.BOLD, 15));
		label.setBounds(540, 140, 80, 43);
		addpageFrame.getContentPane().add(label);
		
		textField = new JTextField();
		textField.setDocument(new MyDocument(11));
		JButton button = new JButton("提交");
		textField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent event) {
				char keyChar = event.getKeyChar();
				if (!(keyChar >= '\u4E00' && keyChar <= '\u9FA5')) return ;
				/*Toolkit.getDefaultToolkit().beep();*/ //声音提示
				event.consume();
			}
			@Override
			public void keyPressed(KeyEvent e) {
				if(e.getKeyCode()==KeyEvent.VK_ENTER)
					button.doClick();
			}
		});
		textField.setColumns(10);
		textField.setBounds(626, 151, 165, 21);
		addpageFrame.getContentPane().add(textField);
		
		JLabel label_1 = new JLabel("姓名：");
		label_1.setForeground(Color.DARK_GRAY);
		label_1.setFont(new Font("幼圆", Font.BOLD, 15));
		label_1.setBounds(540, 190, 80, 43);
		addpageFrame.getContentPane().add(label_1);
		
		textField_1 = new JTextField();
		textField_1.setColumns(10);
		textField_1.setBounds(626, 201, 165, 21);
		addpageFrame.getContentPane().add(textField_1);
		
		JLabel label_2 = new JLabel("年龄：");
		label_2.setForeground(Color.DARK_GRAY);
		label_2.setFont(new Font("幼圆", Font.BOLD, 15));
		label_2.setBounds(540, 240, 80, 43);
		addpageFrame.getContentPane().add(label_2);
		
		textField_2 = new JTextField();
		textField_2.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent event) {
				char keyChar = event.getKeyChar();
				if (!(keyChar >= '\u4E00' && keyChar <= '\u9FA5')) return ;
				/*Toolkit.getDefaultToolkit().beep();*/ //声音提示
				event.consume();
			}
		});
		textField_2.setColumns(10);
		textField_2.setBounds(626, 251, 165, 21);
		addpageFrame.getContentPane().add(textField_2);
		
		JLabel label_3 = new JLabel("职称：");
		label_3.setForeground(Color.DARK_GRAY);
		label_3.setFont(new Font("幼圆", Font.BOLD, 15));
		label_3.setBounds(540, 290, 80, 43);
		addpageFrame.getContentPane().add(label_3);
		
		textField_3 = new JTextField();
		textField_3.setColumns(10);
		textField_3.setBounds(626, 301, 165, 21);
		addpageFrame.getContentPane().add(textField_3);
		
		JLabel label_4 = new JLabel("教授课程：");
		label_4.setForeground(Color.DARK_GRAY);
		label_4.setFont(new Font("幼圆", Font.BOLD, 15));
		label_4.setBounds(540, 390, 80, 43);
		addpageFrame.getContentPane().add(label_4);
		
		textField_4 = new JTextField();
		textField_4.setColumns(10);
		textField_4.setBounds(626, 401, 165, 21);
		addpageFrame.getContentPane().add(textField_4);
		
		JLabel label_5 = new JLabel("联系电话：");
		label_5.setForeground(Color.DARK_GRAY);
		label_5.setFont(new Font("幼圆", Font.BOLD, 15));
		label_5.setBounds(540, 440, 80, 43);
		addpageFrame.getContentPane().add(label_5);
		
		textField_5 = new JTextField();
		textField_5.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent event) {
				char keyChar = event.getKeyChar();
				if (!(keyChar >= '\u4E00' && keyChar <= '\u9FA5' && keyChar=='-')) return ;
				/*Toolkit.getDefaultToolkit().beep();*/ //声音提示
				event.consume();
			}
		});
		textField_5.setColumns(10);
		textField_5.setBounds(626, 451, 165, 21);
		addpageFrame.getContentPane().add(textField_5);
		
		JLabel label_6 = new JLabel("联系地址：");
		label_6.setForeground(Color.DARK_GRAY);
		label_6.setFont(new Font("幼圆", Font.BOLD, 15));
		label_6.setBounds(540, 490, 80, 43);
		addpageFrame.getContentPane().add(label_6);
		

		
		textField_6 = new JTextField();
		textField_6.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if(e.getKeyCode()==KeyEvent.VK_ENTER)
					button.doClick();
			}
		});
		textField_6.setColumns(10);
		textField_6.setBounds(626, 501, 430, 21);
		addpageFrame.getContentPane().add(textField_6);
		
		JLabel label_7 = new JLabel("所在系：");
		label_7.setForeground(Color.DARK_GRAY);
		label_7.setFont(new Font("幼圆", Font.BOLD, 15));
		label_7.setBounds(540, 340, 80, 43);
		addpageFrame.getContentPane().add(label_7);
		
		textField_7 = new JTextField();
		textField_7.setColumns(10);
		textField_7.setBounds(626, 351, 165, 21);
		addpageFrame.getContentPane().add(textField_7);
		
	
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(textField.getText().length()>0) { 
					/*if(textField.getText().length()>11) 
						JOptionPane.showMessageDialog(null, "工号不得超过十一位！", "error！", JOptionPane.ERROR_MESSAGE);	
					else */
					if(!TeacherDao.checkId(textField.getText())) {
						/*System.out.print(textField.getText());*/
						TeacherDao.insert(textField.getText(), textField_1.getText(), textField_2.getText(), textField_3.getText(), textField_7.getText(), textField_4.getText(), textField_5.getText(),
								textField_6.getText());	
						JOptionPane.showMessageDialog(null, "成功插入！", "success！", JOptionPane.PLAIN_MESSAGE);
						Timer timer = new Timer();
					        timer.schedule(new TimerTask() {  
					            public void run() {  
					            	TeacherHomepage th=new TeacherHomepage(id);
									th.teacherHomepageFrame.setVisible(true);
									addpageFrame.dispose();
					            }  
					        }, 500);	
					}
					else JOptionPane.showMessageDialog(null, "数据库中已有此工号！", "error！", JOptionPane.ERROR_MESSAGE);	
				}
				else JOptionPane.showMessageDialog(null, "工号为空！", "error！", JOptionPane.ERROR_MESSAGE);
			}
		});
		button.setForeground(Color.DARK_GRAY);
		button.setFont(new Font("幼圆", Font.BOLD, 16));
		button.setBounds(620, 580, 130, 35);
		addpageFrame.getContentPane().add(button);
		
		JLabel label_8 = new JLabel("，请输入要录入的教师信息！");
		label_8.setForeground(Color.DARK_GRAY);
		label_8.setFont(new Font("幼圆", Font.PLAIN, 12));
		label_8.setBounds(169, 37, 193, 15);
		addpageFrame.getContentPane().add(label_8);
	}
}
